package com.liming.common.result;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.liming.common.enums.ResultEnum;
import com.liming.common.serializer.LMResultSerializer;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;

/**
 * 返回结果
 *
 * @author liming
 * @date 2025/1/3
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "LM自定义返回结果")
@JsonSerialize(using = LMResultSerializer.class)
public class LMResult<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    @Schema(description = "状态码")
    private int code;

    @Schema(description = "提示信息")
    private String message;

    @Schema(description = "返回数据")
    private T data;

    public LMResult(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMsg();
    }

    public LMResult(ResultEnum resultEnum, T data) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMsg();
        this.data = data;
    }

    public static <T> LMResult<T> success() {
        return success(null);
    }

    public static <T> LMResult<T> success(T data) {
        return new LMResult<>(ResultEnum.SUCCESS, data);
    }

    public static <T> LMResult<T> success(String message, T data) {
        LMResult<T> result = success(data);
        result.setMessage(message);
        return result;
    }

    public static <T> LMResult<T> error() {
        return new LMResult<>(ResultEnum.ERROR);
    }

    public static <T> LMResult<T> error(String msg) {
        LMResult<T> result = error();
        result.setMessage(msg);
        return result;
    }

    public static <T> LMResult<T> feignError() {
        return new LMResult<>(ResultEnum.FEIGN_ERROR);
    }

    public static <T> LMResult<T> feignError(String msg) {
        LMResult<T> result = feignError();
        result.setMessage(msg);
        return result;
    }
}
